Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tanya – Pine #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Tanya – Pine #25

wants to merge 2 commits into from

Conversation

tt-ht
Copy link

@tt-ht tt-ht commented Jun 8, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps maintain max or min heap property
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? Each node is compared to its parent at every level, so it is only compared to half the tree
Were the heap_up & heap_down methods useful? Why? Yes, for recursion

Copy link

@kyra-patton kyra-patton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨💫 Nice work! I left a few comments below.

For the comprehension questions,

How is a heap different from a BST? --> Your answer is correct, but what is the max/min heap property?
Were the heap_up & heap_down methods useful? Why? --> You do use recursion in heap_up and heap_down but why is recursion useful here?

Let me know what questions you have.

🟢


def heap_sort(list):
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Time complexity is going to be O(nlogn) as you iterate through n items in list twice, and within each loop perform add or remove which are both O(log n) operations

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice however space complexity is going to be O(log n) here because of the recursive call stack of heap_up

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ However space complexity here will be O(log n) because of the recursive call stack of heap_down

Time complexity: ?
Space complexity: ?
Time complexity: O(1)
Space complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time complexity: ?
Space complexity: ?
Time complexity: O(log n)
Space complexity: O(log n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +100 to +107
if len(self.store) > (left_child) and self.store[index].key > self.store[left_child].key:
self.swap(index, (left_child))
self.heap_down((left_child))
self.heap_down(index)
elif len(self.store) > (right_child) and self.store[index].key > self.store[right_child].key:
self.swap(index, (right_child))
self.heap_down((right_child))
self.heap_down(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reduce your recursive calls to heap_down here. Instead of choosing to swap with the left child automatically if it exists, choose to swap index with whichever child is smaller.

If you swap with the smaller of the two children, that child (as the new parent of index and the other child) will automatically be smaller than both of it's new children and you won't have to call heap_down on it again.


if self.store[parent_index].key > self.store[index].key:
self.swap(parent_index, index)
self.heap_up(parent_index)

def heap_down(self, index):
""" This helper method takes an index and
moves the corresponding element down the heap if it's
larger than either of its children and continues until
the heap property is reestablished.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but review my comment below to see how you might improve upon this solution ⬇️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants